第11回: Python⑤
前回授業の復習
データ構造
list型
1つの変数に、順序性を維持しながら複数のデータを格納できる
code:list.py
import math
import random
students = 'sakitsu', 'saji', 'shimada'
# random.random() → 0以上1未満のランダムな小数を出力 e.g. 0.0012534
# random.random() * len(students) → 配列の長さを掛けることで、0以上3未満のランダムな小数を得る
# math.floor() → 床関数 = 小数点以下切り捨て → 0, 1, 2のいずれかの値を得ることができる
targetIndex = math.floor(random.random() * len(students))
print('今日の掃除当番は君だ!', studentstargetIndex)
cards = '関羽', '張飛', '邢道栄'
weight = 1, 1, 1000
random.choices(cards, k=1000,weights = weight)
dictionary型
list型ではindex(何番目のデータを取り出すか)しか指定できない
dictionary型を活用すると、任意の文字列をindexにデータを格納/取り出しできる
code:dict.py
students = {
'1年': {
'A組' : 'Alice',
'B組' : 'Bob',
'特進' : 'Carol',
},
'2年': {
'A組' : 'Dave',
'B組' : 'Ellen',
'特進' : 'Frank',
}
}
students = 'Alice','Bob','Carol'], [
# 1年A組を取り出したい
students00
grade = input('学年を入力')
clazz = input('組を入力')
print(studentsgradeclazz)
students'1年''A組'
len(1,2,3)
print('何かを出力する')
import math
math.sqrt(2) # 平方根を出力
関数の定義
Python